#ifndef LIBVZCONN_CONN_IPCCONNECT_H_
#define LIBVZCONN_CONN_IPCCONNECT_H_

#include "vzconn/base/basicdefines.h"
#include "vzconn/base/bytebuffer.h"
#include "vzconn/conn/vzconnect.h"

namespace vzconn {

#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
class IpcConnect : public VzConnect {
 public:
  virtual ~IpcConnect();

  // Thread safe
  virtual bool Start();

  bool IsConnected() const {
    return state_ == CS_CONNECTED && socket_.is_open();
  }

  boost::asio::local::stream_protocol::stream_protocol::socket& socket() {
    return socket_;
  }

  virtual const std::string ip_addr() {
    return remote_address_;
  }

 private:
  virtual void AsyncReadData();
  virtual void ConnectError(const boost::system::error_code& err);
  virtual void TryToWriteData();
  virtual void HandleCloseConnect();
 private:
  friend class IpcServer;
  friend class VzClient;
  IpcConnect(boost::asio::io_service& io_service);
 private:
  boost::asio::local::stream_protocol::stream_protocol::socket socket_;
  std::string remote_address_;
};

////////////////////////////////////////////////////////////////////////////////

class BlockVzIpcConnect : public BlockVzConnect {
 public:
  virtual ~BlockVzIpcConnect();

  virtual void CloseConnect();

  virtual bool IsConnected() {
    return socket_.is_open();
  }
 private:
  BlockVzIpcConnect(boost::asio::io_service &io_service);
 private:
  friend class VzClient;
  virtual bool RealySyncWrite(std::vector<boost::asio::const_buffer> &buffers);
  virtual bool RealySyncRead(char *buffer, uint32 buffer_size);
 private:
  boost::asio::local::stream_protocol::stream_protocol::socket socket_;
};

#endif
}

#endif // LIBVZCONN_CONN_IPCCONNECT_H_